package mycoffee.order.model;

import java.util.ArrayList;
import java.util.UUID;

public class Order extends Root {
    private static final long serialVersionUID = 1L;
    private String mOwner;
    private String mCreated;
    private String mEnd;
    private int mStatus;
    private int mShift;
    private String mTableName;
    private String mCashier;
    private int mAreaID;
    private String mAreaName;
    private int mOrderCreatorID;
    private int mTableID;
    private double mTotal;
    private String mOrderCreatorName;
    private Discount mDiscount;
    private String mNote;
    private ArrayList<OrdersDetail> mOrdersDetails;
    
    private boolean sent2Server;

    /**
     * Constructor with no input parameter
     */
    public Order() {
        super();
        mOrdersDetails = new ArrayList<OrdersDetail>();
        this.sent2Server = false;
        this.mId = UUID.randomUUID().hashCode();
    }

    public Order(Order order) {
        this.mOwner = order.getOwner();
        this.mCreated = order.getCreated();
        this.mEnd = order.getEnd();
        this.mStatus = order.getStatus();
        this.mShift = order.getShift();
        this.mTableName = order.getTable();
        this.mCashier = order.getCashier();
        this.mAreaID = order.getAreaID();
        this.mAreaName = order.getAreaName();
        this.mOrderCreatorID = order.getOrderCreatorID();
        this.mTableID = order.getTableID();
        this.mTotal = order.getTotal();
        this.mOrderCreatorName = order.getOrderCreatorName();
        this.mDiscount = order.getDiscount() == null ? null : new Discount(order.getDiscount());
        this.mNote = order.getNote();
        this.mOrdersDetails = new ArrayList<OrdersDetail>();
        if (order.getOrdersDetails() != null) {
        	for (OrdersDetail orderDetail : order.getOrdersDetails()) {
        		this.mOrdersDetails.add(new OrdersDetail(orderDetail));
        	}
        }

        this.sent2Server = false;
    }

    public String getOwner() {
        return mOwner;
    }

    public void setOwner(String owner) {
        mOwner = owner;
    }

    public boolean getSent2Server() {
        return this.sent2Server;
    }

    public void setSent2Server(boolean sent) {
        this.sent2Server = sent;
    }

    public String getCreated() {
        return mCreated;
    }

    public void setCreated(String created) {
        mCreated = created;
    }

    public String getEnd() {
        return mEnd;
    }

    public void setEnd(String end) {
        mEnd = end;
    }

    public int getStatus() {
        return mStatus;
    }

    public void setStatus(int status) {
        mStatus = status;
    }

    public int getShift() {
        return mShift;
    }

    public void setShift(int shift) {
        mShift = shift;
    }

    public String getTable() {
        return mTableName;
    }

    public void setTable(String table) {
        mTableName = table;
    }

    public String getCashier() {
        return mCashier;
    }

    public void setCashier(String cashier) {
        mCashier = cashier;
    }

    public String getAreaName() {
    	return this.mAreaName;
    }
    
    public void setAreaName( String areaName ) {
    	this.mAreaName = areaName;
    }
    
    public int getAreaID() {
        return mAreaID;
    }

    public void setAreaID(int areaID) {
        mAreaID = areaID;
    }

    public int getOrderCreatorID() {
        return mOrderCreatorID;
    }

    public void setOrderCreatorID(int orderCreatorID) {
        mOrderCreatorID = orderCreatorID;
    }

    public int getTableID() {
        return mTableID;
    }

    public void setTableID(int tableID) {
        mTableID = tableID;
    }

    public double getTotal() {
        return mTotal;
    }

    public void setTotal(double total) {
        mTotal = total;
    }

    public String getOrderCreatorName() {
        return mOrderCreatorName;
    }

    public void setOrderCreatorName(String orderCreatorName) {
        mOrderCreatorName = orderCreatorName;
    }

    public Discount getDiscount() {
        return mDiscount;
    }

    public void setDiscount(Discount discount) {
        mDiscount = discount;
    }

    public String getNote() {
        return mNote;
    }

    public void setNote(String note) {
        mNote = note;
    }

    /**
     * @return the ordersDetails
     */
    public ArrayList<OrdersDetail> getOrdersDetails() {
        return mOrdersDetails;
    }

    /**
     * @param ordersDetails
     *            the ordersDetails to set
     */
    public void setOrdersDetails(ArrayList<OrdersDetail> ordersDetails) {
        mOrdersDetails = ordersDetails;
    }

    /**
     * process order a food.
     * 
     * @param food
     */
    public void orderForFood(Food food) {
        if (food == null || mOrdersDetails == null) {
            return;
        }

        // this flag to know this food is new food in list order.
        boolean isNewOrder = true;
        for (int i = 0; i < mOrdersDetails.size(); i++) {
            OrdersDetail ordersDetail = mOrdersDetails.get(i);
            if (ordersDetail.getFoodID() == food.getId()) {
                isNewOrder = false;
                // update count for this order.
                ordersDetail.setAmount(ordersDetail.getAmount() + 1);
                break;
            }
        }

        if (isNewOrder) {
            // add new order detail for this food.
            OrdersDetail ordersDetail = new OrdersDetail();
            ordersDetail.setOrdersID(this.getId());
            ordersDetail.setFood(food);
            ordersDetail.setAmount(1);
            mOrdersDetails.add(ordersDetail);
        }

        // update total money.
        mTotal += food.getPrice();
    }

    /**
     * increase order detail at position of list order details.
     * 
     * @param pos
     */
    public void increaseOrderDetail(int pos) {
        if (mOrdersDetails == null || pos < 0 || pos >= mOrdersDetails.size()) {
            return;
        }
        OrdersDetail ordersDetail = mOrdersDetails.get(pos);
        ordersDetail.setAmount(ordersDetail.getAmount() + 1);
        // update total money.
        mTotal += ordersDetail.getFood().getPrice();
    }

    /**
     * decrease order detail at position of list order details.
     * 
     * @param pos
     */
    public void decreaseOrderDetail(int pos) {
        if (mOrdersDetails == null || pos < 0 || pos >= mOrdersDetails.size()) {
            return;
        }
        OrdersDetail ordersDetail = mOrdersDetails.get(pos);
        ordersDetail.setAmount(ordersDetail.getAmount() - 1);
        if (ordersDetail.getAmount() == 0) {
            // remove this order detail from order.
            mOrdersDetails.remove(pos);
        }
        // update total money.
        mTotal -= ordersDetail.getFood().getPrice();
    }

    /**
     * get number times that order for the food.
     * 
     * @param food
     * @return
     */
    public int getAmountForFood(Food food) {
        int amount = 0;
        for (int i = 0; i < mOrdersDetails.size(); i++) {
            OrdersDetail ordersDetail = mOrdersDetails.get(i);
            if (ordersDetail != null
                    && ordersDetail.getFoodID() == food.getId()) {
                amount = ordersDetail.getAmount();
                break;
            }
        }
        return amount;
    }

    /**
     * get the money will pay.
     * 
     * @return
     */
    public double getTotalMoneyWillPay() {
        double discountMoney = 0;
        if (mDiscount != null) {
            discountMoney = mDiscount.getTotalDiscountMoney(mTotal);
        }
        return mTotal - discountMoney;
    }

    public String getInvoiceTitle() {
    	return String.format("%s - %s - %s", this.getAreaName(), this.getTable(), this.getEnd());
    }
}
